package com.hyt.it.ogt.kq.service.govData.service.kz.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.govData.enums.kwgov.KwGovPushType;
import com.hyt.it.ogt.kq.service.gov.feign.ks.model.PushTimeRoomCandidateParam;
import com.hyt.it.ogt.kq.service.govData.service.kz.IExamCandidateService;
import com.hyt.it.ogt.kq.service.govData.service.kz.IPushTimeRoomCandidateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wuyingge
 * @version 1.0.0
 * @ClassName PushTimeRoomCandidateService.java
 * @Description 
 * @createTime 2022年09月05日 16:41:00
 */
@Service
@Slf4j
public class PushTimeRoomCandidateServiceImpl implements IPushTimeRoomCandidateService {

    @Resource
    private IExamCandidateService iExamCandidateService;

    @DS("kz")
    @Override
    public ApiResponse<Object> pushTimeRoomCandidate(PushTimeRoomCandidateParam pushTimeRoomCandidateParam) {
        Long s = System.currentTimeMillis();
        try {
            log.info("政企推送考生请求参数【{}】", pushTimeRoomCandidateParam.toString());
            if (KwGovPushType.INSERT.getCode().equals(pushTimeRoomCandidateParam.getPushType())) {
                iExamCandidateService.pushTimeRoomCandidate(pushTimeRoomCandidateParam);
            } else if (KwGovPushType.DELETE.getCode().equals(pushTimeRoomCandidateParam.getPushType())) {
                Integer code = iExamCandidateService.deleteTimeRoomCandidate(pushTimeRoomCandidateParam.getTaskId());
                if (code.intValue() != 0) {
                    return ApiResponse
                            .builder()
                            .code(ResponseCode.DELETE_TIME_ROOM_CANDIDATE_ERROR.getCode())
                            .build();
                }
            }
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (Exception e) {
            log.error(StringUtils.join(ResponseCode.PUSH_TIME_ROOM_CANDIDATE_ERROR.getCode(), ": "), e);
            return ApiResponse
                    .builder()
                    .code(ResponseCode.PUSH_TIME_ROOM_CANDIDATE_ERROR.getCode())
                    .build();
        } finally {
            Long e = System.currentTimeMillis();
            log.info("政企推送考生总耗时【{}】", e-s);
        }
    }
}
